<template>
	<sw-common-frame :count="dataCount" :loading="loading">
		<template slot="button" v-if="isExistButton">
			<el-button size="small" type="primary" v-hasPermi="batchForceButton" @click="handleAllForceAmend('2')">全部关闭强制</el-button>
			<el-button size="small" type="primary" v-hasPermi="batchForceButton" @click="handleAllForceAmend('1')">全部开启强制</el-button>
		</template>
		<el-form slot="query" size="small" inline v-if="isExistQuery">
			<el-form-item><el-input placeholder="请输入用户账号" clearable v-model="queryData.account" /></el-form-item>
			<el-form-item><el-input placeholder="请输入用户姓名" clearable v-model="queryData.userName" /></el-form-item>
			<el-form-item><el-button type="primary" icon="el-icon-search" @click="handleQueryTableData()">查询</el-button></el-form-item>
		</el-form>
		<el-table slot="table" v-bind="tableConfig" :data="tableData">
			<el-table-column label="序号" type="index" align="center" width="50" />
			<el-table-column label="账号" prop="account" min-width="240" show-overflow-tooltip />
			<el-table-column label="姓名" prop="userName" min-width="200" show-overflow-tooltip />
			<el-table-column label="所属部门" prop="deptName" min-width="200" show-overflow-tooltip />
			<el-table-column label="是否强制" prop="isForce" min-width="80" align="center" :formatter="returnForceStatus" />
			<el-table-column label="操作" fixed="right" :width="handleWidth" v-if="isHasAuthButton">
				<template slot-scope="{ row }">
					<el-button type="text" v-hasPermi="handleButtons.update" @click="handleForceOperate(row)">{{ updateButtonName(row.isForce) }}</el-button>
				</template>
			</el-table-column>
		</el-table>
		<sw-pagination slot="page" :total="dataCount" :page.sync="queryData.page" :limit.sync="queryData.limit" @change="queryTableData" />
	</sw-common-frame>
</template>

<script>
import listMixins from '@/mixins/listMixins';
import { getUserList, forceUpdateAllUserPassword, forceUpdateUserPassword } from '@/api/system/tbSysUser';
export default {
	name: 'forceModifyPass',
	mixins: [listMixins],
	data() {
		return {
			loading: false,
			queryData: { page: 1, limit: 15 },
			dataCount: 0,
			userListData: []
		};
	},
	computed: {
		listFunction() {
			return eval(getUserList);
		},
		batchForceButton() {
			let batchForce = this.buttons.find((n) => n.field === 'batchForce');
			return batchForce?.id;
		},
		frameTopButton() {
			let frameTopBtns = [];
			if (!!this.addedButton) {
				frameTopBtns = [...frameTopBtns, this.addedButton];
			}
			return frameTopBtns;
		}
	},
	methods: {
		updateButtonName(isForce) {
			return `${isForce === '1' ? '关闭' : '开启'}强制`;
		},
		//返回强制状态
		returnForceStatus(row, column, cellValue) {
			let attrs = { size: 'small' };
			if (cellValue === '1') {
				attrs['text'] = '是';
				attrs['type'] = 'success';
			} else if (cellValue === '2') {
				attrs['text'] = '否';
				attrs['type'] = 'danger';
			}
			return this.$createElement('elTag', { attrs }, attrs.text);
		},

		//开启/关闭强制修改
		async handleForceOperate(row) {
			let isForce = row.isForce === '1';
			let params = { id: row.id, isForce: isForce ? '2' : '1' };
			let result = await forceUpdateUserPassword(params).then((res) => res.data);
			if (result.code === '0') {
				this.$showCustomMessage('success', `${isForce ? '关闭' : '开启'}强制修改成功`);
				row.isForce = isForce ? '2' : '1';
			}
		},

		//全部开启/关闭强制修改
		async handleAllForceAmend(value) {
			let isForce = value === '1';
			let result = await forceUpdateAllUserPassword(value).then((res) => res.data);
			if (result.code === '0') {
				this.$showCustomMessage('success', `全部用户${isForce ? '关闭' : '开启'}强制修改成功`);
				this.handleQueryTableData();
			}
		}
	}
};
</script>

<style lang="scss" scoped></style>
